package com.little.g.springcloud.mall.web.manager;

import com.little.g.springcloud.mall.web.task.Task;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Executors;

@Component
public class TaskService {

	private TaskService taskService;

	private DelayQueue<Task> delayQueue = new DelayQueue<Task>();

	@PostConstruct
	private void init() {
		taskService = this;

		Executors.newSingleThreadExecutor().execute(new Runnable() {
			@Override
			public void run() {
				while (true) {
					try {
						Task task = delayQueue.take();
						task.run();
					}
					catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		});
	}

	public void addTask(Task task) {
		if (delayQueue.contains(task)) {
			return;
		}
		delayQueue.add(task);
	}

	public void removeTask(Task task) {
		delayQueue.remove(task);
	}

}
